Rewrite toarray Perl script to Python
authorEmmanuele Bassi <ebassi@gnome.org>
Wed, 14 Feb 2018 15:51:45 +0000 (15:51 +0000)
committerEmmanuele Bassi <ebassi@gnome.org>
Wed, 14 Feb 2018 15:51:45 +0000 (15:51 +0000)
We don't need to shell out to Perl to generate a C array out of a text
file.

gdk/broadway/gen-c-array.py [new file with mode: 0644]
gdk/broadway/meson.build
gdk/broadway/toarray.pl [deleted file]

diff --git a/gdk/broadway/gen-c-array.py b/gdk/broadway/gen-c-array.py
new file mode 100644 (file)
index 0000000..afffda3
--- /dev/null
@@ -0,0 +1,21 @@
+#!/usr/bin/env python3
+
+import argparse
+import sys
+
+parser = argparse.ArgumentParser()
+parser.add_argument('--array-name', help='The name of the array variable')
+parser.add_argument('--output', metavar='FILE', help='Output file',
+                    type=argparse.FileType('w'),
+                    default=sys.stdout)
+parser.add_argument('input', metavar='FILE', help='The input file',
+                    type=argparse.FileType('r'))
+
+args = parser.parse_args()
+
+args.output.write('static const char {}[] = {{\n'.format(args.array_name))
+for line in args.input:
+    for ch in line:
+        args.output.write('  0x{:02x},\n'.format(ord(ch)))
+
+args.output.write('};')
index 614a62378480ac1f612e625394e09412888b5bf8..970b6c8e6c9691c0f3f16e3449416a0611acb520 100644 (file)
@@ -28,11 +28,18 @@ gdk_broadway_public_headers = [
 
 gdk_broadway_deps = [shmlib]
 
+gen_c_array = find_program('gen-c-array.py')
+
 clienthtml_h = custom_target('clienthtml.h',
   input : 'client.html',
   output : 'clienthtml.h',
-  command : [find_program('toarray.pl'), 'client_html', '@INPUT@'],
-  capture : true)
+  command : [
+    gen_c_array,
+    '--array-name=client_html',
+    '--output=@OUTPUT@',
+    '@INPUT@',
+  ],
+)
 
 libgdk_broadway = static_library('gdk-broadway',
   clienthtml_h,
@@ -52,13 +59,18 @@ broadwayd_syslib = os_win32 ? find_library('ws2_32') : shmlib
 broadwayjs_h = custom_target('broadwayjs.h',
   input : ['broadway.js'],
   output : 'broadwayjs.h',
-  command : [find_program('toarray.pl'), 'broadway_js', '@INPUT0@'],
-  capture : true)
+  command : [
+    gen_c_array,
+    '--array-name=broadway_js',
+    '--output=@OUTPUT@',
+    '@INPUT0@',
+  ],
+)
 
 executable('gtk4-broadwayd',
   clienthtml_h, broadwayjs_h,
   'broadwayd.c', 'broadway-server.c', 'broadway-output.c',
-  include_directories: [confinc, gdkinc],
+  include_directories: [confinc, gdkinc, include_directories('.')],
   c_args: ['-DGDK_COMPILATION', '-DG_LOG_DOMAIN="Gdk"', ],
   dependencies : [broadwayd_syslib, gdk_deps],
   install : true)
diff --git a/gdk/broadway/toarray.pl b/gdk/broadway/toarray.pl
deleted file mode 100755 (executable)
index 642ea8a..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-#!/usr/bin/env perl
-
-use warnings;
-
-my $ARRAYNAME = $ARGV[0];
-my $first = 0;
-print "static const char $ARRAYNAME\[\] = {";
-
-for ($i = 1; $i <= $#ARGV; $i = $i + 1) {
-    my $FILENAME = $ARGV[$i];
-    open FILE, $FILENAME or die "Cannot open $FILENAME";
-    while (my $line = <FILE>) {
-        foreach my $c (split //, $line) {
-            if ($first == 1) {
-                printf (",\n");
-            }
-            printf ("0x%02x", ord ($c));
-            $first = 1;
-        }
-    }
-}
-
-print "};\n";
-